#include	"unp.h"
#include "var.h"

int main() {
	fprintf(stderr,"this is ftp client\n");
	int n = 0;
	int requestNum = 0;
	char buf[MAXLINE];
	char command[MAXLINE];
	int sockfd;
	int filesockfd;
	int server_response = 0;
	CURRENT_STATE = USER_COMMAND;
	ACTIVE_MODE = ON;
	PASSIVE_PORT_NUM = INIT_PASSIVE_PORT;
	DEBUG_MODE = OFF;
	BINARY_MODE = OFF;

	for (;;) {
		if (CURRENT_STATE == USER_COMMAND) {
			fprintf(stderr, "=USER_COMMAND=\n");
			/*initialize variable*/
			DATA_CONN_MODE = OFF;
			memset(&buf,0x00,sizeof(buf));
			memset(&command,0x00,sizeof(command));
			/*initialize variable*/
			n = Read(0, buf, MAXLINE);
			requestNum = makeClientCommand(buf,command);
			/* state transition behavior */
			if(requestNum == OPEN) {
				if ((sockfd = connectFTPServ(21)) != -1)
						CURRENT_STATE = I_REPLY;
			}else if(requestNum == LS || requestNum == TYPE || requestNum == SYSTEM)  {
				CURRENT_STATE = S_REPLY1;
				setServerAsciiMode(sockfd);
			}else if(requestNum == PWD || requestNum == CD || requestNum == CLOSE) {
				CURRENT_STATE = S_REPLY;
				execCommand(sockfd,requestNum,command);
			}else if(requestNum == GET || requestNum == PUT) {
				CURRENT_STATE = S_REPLY;
				setServerConnMode(sockfd);
			}else if(requestNum == QUIT) {
				execCommand(sockfd,requestNum,command);
				CURRENT_STATE = S_REPLY;
				exit(1);
			}else if(requestNum == PASSIVE)  {
				fprintf(stderr,"PASSIVE MODE");
				if(ACTIVE_MODE == ON) {
					printf("PASSIVE MODE ON\n");
					ACTIVE_MODE = OFF;
				}
				else if(ACTIVE_MODE == OFF) {
					printf("ACTIVE MODE ON\n");
					ACTIVE_MODE =ON;
				}
			}else if(requestNum == DEBUG) {
				if(DEBUG_MODE == ON) {
					fprintf(stderr,"debug mode off\n");
					DEBUG_MODE = OFF;
				}
				else if(DEBUG_MODE == OFF) {
					fprintf(stderr,"debug mode on\n");
					DEBUG_MODE =ON;
				}
			}else if(requestNum == TYPE_CHANGE) {
				execCommand(sockfd,requestNum,command);
			}
			/* state transition behavior */
		}
		else if (CURRENT_STATE == I_REPLY) {
			fprintf(stderr, "=I_REPLY=\n");
			server_response = getServerResponse(sockfd);
			if(server_response == 2) {
				fprintf(stderr,"input ID\n");
				execCommand(sockfd,requestNum,command);
				requestNum = PASSWORD;
				CURRENT_STATE = I_REPLY;
			}else if (server_response == 3) {
				fprintf(stderr,"input password\n");
				execCommand(sockfd,requestNum,command);
				CURRENT_STATE = S_REPLY;
			} else {
				CURRENT_STATE = USER_COMMAND;
			}
		} else if (CURRENT_STATE == S_REPLY) {
			fprintf(stderr, "=S_REPLY=\n");
			server_response = getServerResponse(sockfd);
			if (DATA_CONN_MODE == OFF) {
				CURRENT_STATE = USER_COMMAND;
			} else if (DATA_CONN_MODE == ON && server_response == 2 && ACTIVE_MODE == OFF) {
				filesockfd = connectFTPDataServ();
				execCommand(sockfd,requestNum,command);
				CURRENT_STATE = P_REPLY;
			} else if(DATA_CONN_MODE == ON && server_response == 2 && ACTIVE_MODE == ON) {
				execCommand(sockfd,requestNum,command);
				filesockfd = connectFTPDataServ();
				CURRENT_STATE = P_REPLY;
			}

		}else if (CURRENT_STATE == S_REPLY1) {
			fprintf(stderr, "=S_REPLY1=\n");
			server_response = getServerResponse(sockfd);
			if(server_response == 2) {
				filesockfd=setServerConnMode(sockfd);
				CURRENT_STATE = S_REPLY;
			}else {
				CURRENT_STATE = USER_COMMAND;
			}
		} else if (CURRENT_STATE == P_REPLY) {
			fprintf(stderr, "=P_REPLY=\n");
			server_response = getServerResponse(sockfd);
			if(server_response == 1) {
				CURRENT_STATE = DATA_TRANSFER;
				getServerFTPData(filesockfd,requestNum);
			}
			else if(server_response == 2) {
				CURRENT_STATE = USER_COMMAND;
				close(filesockfd);
			}
			else if(server_response == 5) {
				CURRENT_STATE = USER_COMMAND;
			}
		} else if(CURRENT_STATE == DATA_TRANSFER) {
			fprintf(stderr, "=DATA_TRANSFER=\n");
			close(filesockfd);
			server_response = getServerResponse(sockfd);
			if(server_response == 2) {
				CURRENT_STATE = USER_COMMAND;
			}
		}
	}
}

